package yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;

import yunhe.biz.IProcureBiz;
import yunhe.biz.impl.ProcureBizImpl;
import yunhe.entity.Product;
import yunhe.entity.Purchase;
import yunhe.entity.PurchaseAppend;
import yunhe.entity.Supplier;
import yunhe.util.GeneralUtil;
@WebServlet(name="SelectPurchaseHistoryRecord",urlPatterns="/SelectPurchaseHistoryRecord")
public class SelectPurchaseHistoryRecord extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String datemin=request.getParameter("datemin");
		String datemax=request.getParameter("datemax");
		IProcureBiz ipb=new ProcureBizImpl();
		String [] values=request.getParameterValues("condition");
		String choose=request.getParameter("result");
		Supplier supplier=new Supplier();
		Product product=new Product();
		List<PurchaseAppend> purchaseAppendList=new ArrayList<PurchaseAppend>();
		
		int count=0;
		if(values[0].equals("1")){
			supplier.setSupName(choose);
			supplier=ipb.selectSupplierBySupplier(supplier);
			purchaseAppendList=ipb.selectPurchaseAppendBySupplierPagesDate(datemin, datemax, supplier);
			count=ipb.selectPurchaseAppendBySupplierPagesCount(datemin, datemax, supplier);
			/*if(purchaseAppendList!=null){
				request.setAttribute("currPage", currPage);
				request.setAttribute("totalPages", totalPages);
				request.setAttribute("purchaseAppendList", purchaseAppendList);
				request.getRequestDispatcher("/purchase/historyRecord.jsp").forward(request, response);
			}*/
			
		}else if(values[0].equals("2")){
			product.setProBarCode(choose);
			purchaseAppendList=ipb.selectPurchaseAppendByProductDate(datemin, datemax, product);
			count=ipb.selectPurchaseAppendByProductDateCount(datemin, datemax, product);
			/*if(purchaseAppendList!=null){
				request.setAttribute("currPage", currPage);
				request.setAttribute("totalPages", totalPages);
				request.setAttribute("purchaseAppendList", purchaseAppendList);
				request.getRequestDispatcher("/purchase/historyRecord.jsp").forward(request, response);
			}*/
		}
		JSONArray ja=new JSONArray();
		for(int i=0;i<purchaseAppendList.size();i++){
			Map<String,Object> inMap=new HashMap<String,Object>();
			PurchaseAppend pa=purchaseAppendList.get(i);
				inMap.put("pursId", pa.getPurchase().getPursId());
				inMap.put("supName",pa.getPurchase().getSupplier().getSupName());
				inMap.put("purchaseDate",pa.getPurchase().getPutchaseDate());
				inMap.put("operatorName",pa.getPurchase().getOperator().getName());
				inMap.put("docMakerName", pa.getPurchase().getDocMaker().getName());
				inMap.put("checkerName", pa.getPurchase().getChecker().getName());
				inMap.put("keeperName", pa.getPurchase().getKeeper().getName());
				inMap.put("accountPaying", pa.getPurchase().getAccountPaying());
				inMap.put("accountPaied", pa.getPurchase().getAccountPaied());
				inMap.put("executeState", pa.getPurchase().getExcuteState());
				inMap.put("purId",pa.getPurId());
			ja.put(inMap);
		}
		PrintWriter out=response.getWriter();
		if(ja.length()>0){
			out.print(ja.toString());
		}else{
         out.print(false);
		}
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	
		doGet(req, resp);
	}
}
